"use strict";
const mvc = require('cosjs.mvc');

/**
 * @name talent
 * 天赋树
 */

/**
 * @name setTalent
 * @param int rootNode 根节点ID
 * @param int talentID 普通节点ID
 * 解锁天赋点
 */
exports.setTalent = async function () {
    let rootNode = this.get('rootNode', 'int');
    let talentID = this.get('talentID', 'int');

    if (!rootNode || !talentID) {
        return this.quick_error('参数错误', rootNode, talentID);
    }

    let playerTalentConfig = this.config('PlayerTalent', talentID);
    if (!playerTalentConfig) {
        return this.quick_error('天赋点配置不存在', talentID);
    }
    let condition = playerTalentConfig.Cond;
    this.updater.role.key('talentTree');
    this.updater.role.key('useTalentItem'); //记录消耗天赋点

    await this.updater.data();
    let talentTree = this.updater.role.get('talentTree') || {};
    let useTalentItem = this.updater.role.get('useTalentItem');

    let beforeTalentNode = playerTalentConfig.beforeTalent;
    //解锁技能节点(根结点)
    if (!beforeTalentNode) {
        if (talentTree[rootNode]) {
            return this.quick_error('技能节点（根结点）已解锁', rootNode);
        }
        for (let item of condition) {
            if (item.Condition == 'talentPoint') {
                let talentPointId = parseInt(this.config('base', 'talentPoint').val);
                if (!talentPointId) {
                    return this.quick_error('基础配置talentPoint不存在', 'talentPoint');
                }
                if (useTalentItem[talentPointId] == undefined) {
                    useTalentItem[talentPointId] = 0;
                }
                let num = parseInt(item.Param);
                useTalentItem[talentPointId] += num;
                this.updater.item.sub(talentPointId, num);
                this.updater.role.set('useTalentItem', useTalentItem);
            } else if (item.Condition == 'plotFinish') {
                let stageCondition = item.Param;
                let chapterID = Math.floor(stageCondition / 100);

                this.updater.mainStory.key(chapterID);
                await this.updater.data();
                let stageData = this.updater.mainStory.get(chapterID).stageData;
                if (!stageData) {
                    return this.quick_error('条件关卡未通过', chapterID);
                }
                // console.log('stageData', stageData, stageCondition)
                if (!stageData.hasOwnProperty(stageCondition) || !stageData[stageCondition].hasOwnProperty('pass') || !stageData[stageCondition].pass) {
                    return this.quick_error('条件关卡未通过，根节点天赋点不能解锁', stageCondition);
                }
            }
        }
    } else if (talentTree[rootNode] == talentID) {
        return this.quick_error("M7GHLB123", '该天赋点已解锁！', talentID);
    } else if (talentTree[rootNode] !== beforeTalentNode) {
        return this.quick_error('M7GHLB123', '前置结点天赋点未解锁', talentID);
    } else {
        //解锁普通结点 消耗天赋点
        for (let item of condition) {
            // let talentPointId = parseInt(this.config('base', 'talentPoint').val);
            // if (!talentPointId) {
            //     return this.quick_error('基础配置talentPoint不存在', 'talentPoint');
            // }
            // if (useTalentItem[talentPointId] == undefined) {
            //     useTalentItem[talentPointId] = 0;
            // }
            // let num = parseInt(item.Param);
            // useTalentItem[talentPointId] += num;
            // this.updater.item.sub(talentPointId, num);
            // this.updater.role.set('useTalentItem', useTalentItem);
            if (item.Condition == 'talentPoint') {
                let talentPointId = parseInt(this.config('base', 'talentPoint').val);
                if (!talentPointId) {
                    return this.quick_error('基础配置talentPoint不存在', 'talentPoint');
                }
                if (useTalentItem[talentPointId] == undefined) {
                    useTalentItem[talentPointId] = 0;
                }
                let num = parseInt(item.Param);
                useTalentItem[talentPointId] += num;
                this.updater.item.sub(talentPointId, num);
                this.updater.role.set('useTalentItem', useTalentItem);
            } else if (item.Condition == 'plotFinish') {
                let stageCondition = item.Param;
                let chapterID = Math.floor(stageCondition / 100);

                this.updater.mainStory.key(chapterID);
                await this.updater.data();
                let stageData = this.updater.mainStory.get(chapterID).stageData;
                if (!stageData) {
                    return this.quick_error('条件关卡未通过', chapterID);
                }
                // console.log('stageData', stageData, stageCondition)
                if (!stageData.hasOwnProperty(stageCondition) || !stageData[stageCondition].hasOwnProperty('pass') || !stageData[stageCondition].pass) {
                    return this.quick_error('条件关卡未通过，根节点天赋点不能解锁', stageCondition);
                }
            }
        }
    }
    talentTree[rootNode] = talentID;
    this.updater.role.set('talentTree', talentTree);

    return this.updater.save();
}


/**
* @name returnTalent
* 重置天赋点
*/
exports.returnTalent = async function () {
    this.updater.role.key('talentTree');
    this.updater.role.key('useTalentItem')
    this.updater.role.key('useTalentBranch');
    await this.updater.data();

    let talentTree = this.updater.role.get('talentTree');
    if (talentTree == {}) {
        return this.quick_error('还未有解锁的技能节点');
    }
    let useTalentItem = this.updater.role.get('useTalentItem');
    // let useTalentBranch = this.updater.role.get('useTalentBranch');
    //遍历天赋树，value设置为根结点ID
    // for (let rootNode in talentTree) {
    //     talentTree[rootNode] = rootNode;
    // }
    // for (let rootNode in useTalentBranch) {
    //     delete useTalentBranch[rootNode];
    // }
    for (let itemID in useTalentItem) {
        this.updater.item.add(parseInt(itemID), useTalentItem[itemID]);
        delete useTalentItem[itemID];
    }
    for (let rootNode in talentTree) {
        delete talentTree[rootNode];
    }
    this.updater.role.set('useTalentItem', useTalentItem);
    this.updater.role.set('talentTree', talentTree);
    this.updater.role.set('useTalentBranch', []);
    return this.updater.save();
}

/**
 * @name setTalentTreeBranch
 * @param string rootNodeStr 用','分割
 * 装备天赋树分支
 */
exports.setTalentTreeBranch = async function () {
    let ss = this.get('rootNodeStr', 'string');
    let TalentBranchArr = ss.split(',');
    if (ss == '') {
        TalentBranchArr = [];
    }
    this.updater.role.key('talentTree');
    await this.updater.data();
    let talentTree = this.updater.role.get('talentTree') || {};
    // if (TalentBranchArr.length == 0) {
    //     this.updater.role.set('useTalentBranch', []);
    //     return this.updater.save();
    // }
    if (TalentBranchArr.length > 2) {
        return this.quick_error("M7GHLB123", '参数错误', ss);
    }
    for (let rootID of TalentBranchArr) {
        if (!talentTree.hasOwnProperty(rootID)) {
            return this.quick_error('该天赋分支根节点未解锁', rootID);
        }
    }
    this.updater.role.set('useTalentBranch', TalentBranchArr);
    return this.updater.save();
}

/**
 * @name setTeamTalentBranch
 * @param string teamId 编队唯一ID（类型+id）
 * @param string rootNodeStr 根结点ID 用','分割
 * 设置编队的天赋树分支
 */
exports.setTeamTalentBranch = async function () {
    let teamId = this.get('teamId', 'string');
    let ss = this.get('rootNodeStr', 'string');
    if (!teamId) {
        return this.quick_error('teamId 参数错误', teamId);
    }
    let rootNodeList = ss.split(',');
    if (ss == '') {
        rootNodeList = [];
    }
    this.updater.role.key('talentTree');
    this.updater.role.key('teamTalentBranch');
    this.updater.role.key('team.' + teamId);
    await this.updater.data();
    let talentTree = this.updater.role.get('talentTree');
    let teamTalentBranch = this.updater.role.get('teamTalentBranch');
    let teamData = this.updater.role.get('team.' + teamId);

    if (!teamData) {
        return this.quick_error('玩家没有该编队', teamId);
    }
    let teamConfig = this.config('team', teamData.type);
    if (!teamConfig) {
        return this.quick_error('该编队类型配置不存在', teamData.type);
    }
    // if (rootNodeList.length > teamConfig.talentAble) {
    //     return this.quick_error('不符合编队装备天赋树分支数量要求', rootNodeList.length, '配置要求：<=' + teamConfig.talentAble);
    // }
    for (let rootID of rootNodeList) {
        if (!talentTree.hasOwnProperty(rootID)) {
            return this.quick_error('该天赋分支的根节点未解锁', rootID);
        }
    }

    teamTalentBranch[teamId] = rootNodeList;

    this.updater.role.set('teamTalentBranch', teamTalentBranch);
    return this.updater.save();

}